// pages/video/video.js - 严格按照test示例设计规范
Page({
  data: {
    videoInfo: {
      id: 1,
      title: '全身燃脂·高效训练',
      url: 'https://example.com/video.mp4',
      duration: 1500, // 25分钟，单位：秒
      views: 1200,
      likes: 86,
      chapters: [
        { id: 1, title: '热身准备', duration: '3分钟', completed: true },
        { id: 2, title: '核心激活', duration: '5分钟', completed: true },
        { id: 3, title: '全身燃脂', duration: '12分钟', completed: false },
        { id: 4, title: '力量训练', duration: '8分钟', completed: false },
        { id: 5, title: '有氧训练', duration: '6分钟', completed: false },
        { id: 6, title: '拉伸放松', duration: '4分钟', completed: false }
      ]
    },
    isPlaying: false,
    isMuted: false,
    currentTime: 0,
    progress: 0,
    playbackRate: 1,
    currentChapter: 1,
    chapterProgress: 0,
    showComplete: false,
    currentTip: '保持呼吸节奏，动作要标准，不要急于求成。',
    actionPoints: [
      '保持核心收紧，腹部用力',
      '动作要缓慢控制，感受肌肉发力',
      '保持呼吸顺畅，不要憋气',
      '如果感到不适，立即停止',
      '每组动作之间休息30秒'
    ],
    trainingStats: {
      duration: '25分钟',
      calories: '200卡',
      points: '50分'
    }
  },

  onLoad(options) {
    console.log('视频页面加载完成', options);
    const { id, chapterId } = options;
    
    if (id) {
      this.loadVideoInfo(id);
    }
    
    if (chapterId) {
      this.setData({
        currentChapter: parseInt(chapterId)
      });
    }
  },

  onUnload() {
    // 页面卸载时保存进度
    this.saveProgress();
  },

  // 加载视频信息
  loadVideoInfo(id) {
    // 这里可以根据id从服务器加载视频信息
    console.log('加载视频信息', id);
  },

  // 视频播放事件
  onVideoPlay() {
    this.setData({
      isPlaying: true
    });
  },

  // 视频暂停事件
  onVideoPause() {
    this.setData({
      isPlaying: false
    });
  },

  // 视频结束事件
  onVideoEnded() {
    this.setData({
      isPlaying: false,
      showComplete: true
    });
    this.completeTraining();
  },

  // 时间更新事件
  onTimeUpdate(e) {
    const currentTime = e.detail.currentTime;
    const duration = this.data.videoInfo.duration;
    const progress = (currentTime / duration) * 100;
    
    this.setData({
      currentTime: currentTime,
      progress: progress
    });
    
    // 更新章节进度
    this.updateChapterProgress(currentTime);
  },

  // 切换播放/暂停
  togglePlay() {
    this.setData({
      isPlaying: !this.data.isPlaying
    });
  },

  // 切换静音
  toggleMute() {
    this.setData({
      isMuted: !this.data.isMuted
    });
  },

  // 切换全屏
  toggleFullscreen() {
    // 小程序中全屏需要特殊处理
    wx.showToast({
      title: '全屏功能开发中',
      icon: 'none'
    });
  },

  // 切换播放速度
  toggleSpeed() {
    const rates = [0.5, 0.75, 1, 1.25, 1.5, 2];
    const currentIndex = rates.indexOf(this.data.playbackRate);
    const nextIndex = (currentIndex + 1) % rates.length;
    
    this.setData({
      playbackRate: rates[nextIndex]
    });
  },

  // 跳转到指定时间
  seekTo(e) {
    const x = e.detail.x;
    const progress = x / e.currentTarget.offsetWidth;
    const seekTime = progress * this.data.videoInfo.duration;
    
    this.setData({
      currentTime: seekTime,
      progress: progress * 100
    });
  },

  // 播放指定章节
  playChapter(e) {
    const chapterId = e.currentTarget.dataset.id;
    this.setData({
      currentChapter: chapterId
    });
    
    // 这里可以跳转到章节开始时间
    wx.showToast({
      title: '跳转到章节',
      icon: 'success'
    });
  },

  // 更新章节进度
  updateChapterProgress(currentTime) {
    // 根据当前时间计算章节进度
    const chapterDuration = 180; // 3分钟，单位：秒
    const chapterProgress = ((currentTime % chapterDuration) / chapterDuration) * 100;
    
    this.setData({
      chapterProgress: chapterProgress
    });
  },

  // 完成训练
  completeTraining() {
    // 更新训练数据
    const chapters = this.data.videoInfo.chapters.map(chapter => {
      if (chapter.id === this.data.currentChapter) {
        return { ...chapter, completed: true };
      }
      return chapter;
    });
    
    this.setData({
      'videoInfo.chapters': chapters
    });
    
    // 保存训练记录
    this.saveTrainingRecord();
  },

  // 保存训练记录
  saveTrainingRecord() {
    // 这里可以保存训练记录到服务器
    console.log('保存训练记录');
  },

  // 保存进度
  saveProgress() {
    // 这里可以保存观看进度到服务器
    console.log('保存观看进度', this.data.currentTime);
  },

  // 格式化时间
  formatTime(seconds) {
    const minutes = Math.floor(seconds / 60);
    const remainingSeconds = Math.floor(seconds % 60);
    return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  },

  // 分享成果
  shareResult() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    });
  },

  // 继续训练
  nextTraining() {
    this.setData({
      showComplete: false
    });
    
    // 跳转到下一个训练
    wx.navigateTo({
      url: '/pages/course/course'
    });
  },

  // 分享给朋友
  onShareAppMessage() {
    return {
      title: this.data.videoInfo.title,
      path: `/pages/video/video?id=${this.data.videoInfo.id}`,
      imageUrl: 'https://example.com/thumbnail.jpg'
    };
  },

  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: this.data.videoInfo.title,
      imageUrl: 'https://example.com/thumbnail.jpg'
    };
  }
});